<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class ZlYearActiveText extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_year_active_text';

    protected $cacheModel = 'active.year_text';

    /**
     * 获取用户 字 信息
     */
    public function getUserTextInfo(int $activeFlag, int $uid)
    {
        $resData = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $activeFlag . ':' . $uid)
            ->getDataAndSet(function () use ($activeFlag, $uid)
            {
                try
                {
                    $data = $this->where(['active_flag' => $activeFlag, 'uid' => $uid])->column('id,num', 'text');
                    $resData = [];
                    if ($data)
                    {
                        foreach ($data as $text => $info)
                        {
                            $resData[(string)$text] = json_encode($info);
                        }
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            });

        foreach ($resData as $text => $textInfoItem)
        {
            $resData[$text] = json_decode($textInfoItem, true);
        }

        return $resData;
    }

    /**
     * 用户 字 变动
     * @param int $activeFlag
     * @param int $uid
     * @param array $changeData [ id  num  text change_num=>变动数量]
     * @param int $type 类型 1=>签到  2=>赠送  3=>受赠  4=>兑换 5=>分享【2022五一活动开始增加该字段】
     * @param int $sendUid 赠送用户
     * @return bool
     */
    public function userChangeTextInfo(int $activeFlag, int $uid, array $changeData, int $type, int $sendUid = 0)
    {
        try
        {
            $time = time();
            $resChacheData = [];
            $changeRecord = [];
            foreach ($changeData as $changeItem)
            {
                if (isset($changeItem['id']))
                {
                    $this->where('id', $changeItem['id'])->update([
                        'num' => $changeItem['num'],
                        'update_time' => $time
                    ]);

                } else
                {
                    $id = $this->insertGetId([
                        'active_flag' => $activeFlag,
                        'uid' => $uid,
                        'text' => $changeItem['text'],
                        'num' => $changeItem['num'],
                        'create_time' => $time
                    ]);
                    $changeItem['id'] = $id;
                }

                $resChacheData[$changeItem['text']] = json_encode([
                    'id' => $changeItem['id'],
                    'text' => $changeItem['text'],
                    'num' => $changeItem['num']
                ]);

                $changeRecord[] = [
                    'active_flag' => $activeFlag,
                    'uid' => $uid,
                    'text' => $changeItem['text'],
                    'change_num' => $changeItem['change_num'],
                    'type' => $type,
                    'send_uid' => $sendUid,
                    'create_time' => $time
                ];
            }
            // 更新 字 缓存
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $activeFlag . ':' . $uid)->setCache($resChacheData);

            // 写入 字变动缓存
            (new ZlYearActiveTextChangeRecode())->saveAll($changeRecord);
        }
        catch (\Exception $e)
        {
            Log::write("春节活动 集字活动 字更新失败：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);
            return false;
        }

        return true;
    }
}
